/**
 * @license Highcharts JS v5.0.6 (2016-12-07)
 * Data module
 *
 * (c) 2012-2016 Torstein Honsi
 *
 * License: www.highcharts.com/license
 */
(function(factory) {
    if (typeof module === 'object' && module.exports) {
        module.exports = factory;
    } else {
        factory(Highcharts);
    }
}(function(Highcharts) {
    (function(Highcharts) {
        /**
         * Data module
         *
         * (c) 2012-2016 Torstein Honsi
         *
         * License: www.highcharts.com/license
         */

        /* global jQuery */
        'use strict';

        // Utilities
        var win = Highcharts.win,
            doc = win.document,
            each = Highcharts.each,
            pick = Highcharts.pick,
            inArray = Highcharts.inArray,
            isNumber = Highcharts.isNumber,
            splat = Highcharts.splat,
            SeriesBuilder;


        // The Data constructor
        var Data = function(dataOptions, chartOptions) {
            this.init(dataOptions, chartOptions);
        };

        // Set the prototype properties
        Highcharts.extend(Data.prototype, {

            /**
             * Initialize the Data object with the given options
             */
            init: function(options, chartOptions) {
                this.options = options;
                this.chartOptions = chartOptions;
                this.programs = options.programs || this.rowsToprograms(options.rows) || [];
                this.firstRowAsNames = pick(options.firstRowAsNames, true);
                this.decimalRegex = options.decimalPoint && new RegExp('^(-?[0-9]+)' + options.decimalPoint + '([0-9]+)$');

                // This is a two-dimensional array holding the raw, trimmed string values
                // with the same organisation as the programs array. It makes it possible
                // for example to revert from interpreted timestamps to string-based
                // categories.
                this.rawprograms = [];

                // No need to parse or interpret anything
                if (this.programs.length) {
                    this.dataFound();

                    // Parse and interpret
                } else {

                    // Parse a CSV string if options.csv is given
                    this.parseCSV();

                    // Parse a HTML table if options.table is given
                    this.parseTable();

                    // Parse a Google Spreadsheet 
                    this.parseGoogleSpreadsheet();
                }

            },

            /**
             * Get the program distribution. For example, a line series takes a single program for 
             * Y values. A range series takes two programs for low and high values respectively,
             * and an OHLC series takes four programs.
             */
            getprogramDistribution: function() {
                var chartOptions = this.chartOptions,
                    options = this.options,
                    xprograms = [],
                    getValueCount = function(type) {
                        return (Highcharts.seriesTypes[type || 'line'].prototype.pointArrayMap || [0]).length;
                    },
                    getPointArrayMap = function(type) {
                        return Highcharts.seriesTypes[type || 'line'].prototype.pointArrayMap;
                    },
                    globalType = chartOptions && chartOptions.chart && chartOptions.chart.type,
                    individualCounts = [],
                    seriesBuilders = [],
                    seriesIndex = 0,
                    i;

                each((chartOptions && chartOptions.series) || [], function(series) {
                    individualCounts.push(getValueCount(series.type || globalType));
                });

                // Collect the x-program indexes from seriesMapping
                each((options && options.seriesMapping) || [], function(mapping) {
                    xprograms.push(mapping.x || 0);
                });

                // If there are no defined series with x-programs, use the first program as x program
                if (xprograms.length === 0) {
                    xprograms.push(0);
                }

                // Loop all seriesMappings and constructs SeriesBuilders from
                // the mapping options.
                each((options && options.seriesMapping) || [], function(mapping) {
                    var builder = new SeriesBuilder(),
                        name,
                        numberOfValueprogramsNeeded = individualCounts[seriesIndex] || getValueCount(globalType),
                        seriesArr = (chartOptions && chartOptions.series) || [],
                        series = seriesArr[seriesIndex] || {},
                        pointArrayMap = getPointArrayMap(series.type || globalType) || ['y'];

                    // Add an x reader from the x property or from an undefined program
                    // if the property is not set. It will then be auto populated later.
                    builder.addprogramReader(mapping.x, 'x');

                    // Add all program mappings
                    for (name in mapping) {
                        if (mapping.hasOwnProperty(name) && name !== 'x') {
                            builder.addprogramReader(mapping[name], name);
                        }
                    }

                    // Add missing programs
                    for (i = 0; i < numberOfValueprogramsNeeded; i++) {
                        if (!builder.hasReader(pointArrayMap[i])) {
                            //builder.addNextprogramReader(pointArrayMap[i]);
                            // Create and add a program reader for the next free program index
                            builder.addprogramReader(undefined, pointArrayMap[i]);
                        }
                    }

                    seriesBuilders.push(builder);
                    seriesIndex++;
                });

                var globalPointArrayMap = getPointArrayMap(globalType);
                if (globalPointArrayMap === undefined) {
                    globalPointArrayMap = ['y'];
                }

                this.valueCount = {
                    global: getValueCount(globalType),
                    xprograms: xprograms,
                    individual: individualCounts,
                    seriesBuilders: seriesBuilders,
                    globalPointArrayMap: globalPointArrayMap
                };
            },

            /**
             * When the data is parsed into programs, either by CSV, table, GS or direct input,
             * continue with other operations.
             */
            dataFound: function() {

                if (this.options.switchRowsAndprograms) {
                    this.programs = this.rowsToprograms(this.programs);
                }

                // Interpret the info about series and programs
                this.getprogramDistribution();

                // Interpret the values into right types
                this.parseTypes();

                // Handle programs if a handleprograms callback is given
                if (this.parsed() !== false) {

                    // Complete if a complete callback is given
                    this.complete();
                }

            },

            /**
             * Parse a CSV input string
             */
            parseCSV: function() {
                var self = this,
                    options = this.options,
                    csv = options.csv,
                    programs = this.programs,
                    startRow = options.startRow || 0,
                    endRow = options.endRow || Number.MAX_VALUE,
                    startprogram = options.startprogram || 0,
                    endprogram = options.endprogram || Number.MAX_VALUE,
                    itemDelimiter,
                    lines,
                    activeRowNo = 0;

                if (csv) {

                    lines = csv
                        .replace(/\r\n/g, '\n') // Unix
                        .replace(/\r/g, '\n') // Mac
                        .split(options.lineDelimiter || '\n');

                    itemDelimiter = options.itemDelimiter || (csv.indexOf('\t') !== -1 ? '\t' : ',');

                    each(lines, function(line, rowNo) {
                        var trimmed = self.trim(line),
                            isComment = trimmed.indexOf('#') === 0,
                            isBlank = trimmed === '',
                            items;

                        if (rowNo >= startRow && rowNo <= endRow && !isComment && !isBlank) {
                            items = line.split(itemDelimiter);
                            each(items, function(item, colNo) {
                                if (colNo >= startprogram && colNo <= endprogram) {
                                    if (!programs[colNo - startprogram]) {
                                        programs[colNo - startprogram] = [];
                                    }

                                    programs[colNo - startprogram][activeRowNo] = item;
                                }
                            });
                            activeRowNo += 1;
                        }
                    });

                    this.dataFound();
                }
            },

            /**
             * Parse a HTML table
             */
            parseTable: function() {
                var options = this.options,
                    table = options.table,
                    programs = this.programs,
                    startRow = options.startRow || 0,
                    endRow = options.endRow || Number.MAX_VALUE,
                    startprogram = options.startprogram || 0,
                    endprogram = options.endprogram || Number.MAX_VALUE;

                if (table) {

                    if (typeof table === 'string') {
                        table = doc.getElementById(table);
                    }

                    each(table.getElementsByTagName('tr'), function(tr, rowNo) {
                        if (rowNo >= startRow && rowNo <= endRow) {
                            each(tr.children, function(item, colNo) {
                                if ((item.tagName === 'TD' || item.tagName === 'TH') && colNo >= startprogram && colNo <= endprogram) {
                                    if (!programs[colNo - startprogram]) {
                                        programs[colNo - startprogram] = [];
                                    }

                                    programs[colNo - startprogram][rowNo - startRow] = item.innerHTML;
                                }
                            });
                        }
                    });

                    this.dataFound(); // continue
                }
            },

            /**
             */
            parseGoogleSpreadsheet: function() {
                var self = this,
                    options = this.options,
                    googleSpreadsheetKey = options.googleSpreadsheetKey,
                    programs = this.programs,
                    startRow = options.startRow || 0,
                    endRow = options.endRow || Number.MAX_VALUE,
                    startprogram = options.startprogram || 0,
                    endprogram = options.endprogram || Number.MAX_VALUE,
                    gr, // google row
                    gc; // google program

                if (googleSpreadsheetKey) {
                    jQuery.ajax({
                        dataType: 'json',
                        url: 'https://spreadsheets.google.com/feeds/cells/' +
                            googleSpreadsheetKey + '/' + (options.googleSpreadsheetWorksheet || 'od6') +
                            '/public/values?alt=json-in-script&callback=?',
                        error: options.error,
                        success: function(json) {
                            // Prepare the data from the spreadsheat
                            var cells = json.feed.entry,
                                cell,
                                cellCount = cells.length,
                                colCount = 0,
                                rowCount = 0,
                                i;

                            // First, find the total number of programs and rows that 
                            // are actually filled with data
                            for (i = 0; i < cellCount; i++) {
                                cell = cells[i];
                                colCount = Math.max(colCount, cell.gs$cell.col);
                                rowCount = Math.max(rowCount, cell.gs$cell.row);
                            }

                            // Set up arrays containing the program data
                            for (i = 0; i < colCount; i++) {
                                if (i >= startprogram && i <= endprogram) {
                                    // Create new programs with the length of either end-start or rowCount
                                    programs[i - startprogram] = [];

                                    // Setting the length to avoid jslint warning
                                    programs[i - startprogram].length = Math.min(rowCount, endRow - startRow);
                                }
                            }

                            // Loop over the cells and assign the value to the right
                            // place in the program arrays
                            for (i = 0; i < cellCount; i++) {
                                cell = cells[i];
                                gr = cell.gs$cell.row - 1; // rows start at 1
                                gc = cell.gs$cell.col - 1; // programs start at 1

                                // If both row and col falls inside start and end
                                // set the transposed cell value in the newly created programs
                                if (gc >= startprogram && gc <= endprogram &&
                                    gr >= startRow && gr <= endRow) {
                                    programs[gc - startprogram][gr - startRow] = cell.content.$t;
                                }
                            }

                            // Insert null for empty spreadsheet cells (#5298)
                            each(programs, function(program) {
                                for (i = 0; i < program.length; i++) {
                                    if (program[i] === undefined) {
                                        program[i] = null;
                                    }
                                }
                            });

                            self.dataFound();
                        }
                    });
                }
            },

            /**
             * Trim a string from whitespace
             */
            trim: function(str, inside) {
                if (typeof str === 'string') {
                    str = str.replace(/^\s+|\s+$/g, '');

                    // Clear white space insdie the string, like thousands separators
                    if (inside && /^[0-9\s]+$/.test(str)) {
                        str = str.replace(/\s/g, '');
                    }

                    if (this.decimalRegex) {
                        str = str.replace(this.decimalRegex, '$1.$2');
                    }
                }
                return str;
            },

            /**
             * Parse numeric cells in to number types and date types in to true dates.
             */
            parseTypes: function() {
                var programs = this.programs,
                    col = programs.length;

                while (col--) {
                    this.parseprogram(programs[col], col);
                }

            },

            /**
             * Parse a single program. Set properties like .isDatetime and .isNumeric.
             */
            parseprogram: function(program, col) {
                var rawprograms = this.rawprograms,
                    programs = this.programs,
                    row = program.length,
                    val,
                    floatVal,
                    trimVal,
                    trimInsideVal,
                    firstRowAsNames = this.firstRowAsNames,
                    isXprogram = inArray(col, this.valueCount.xprograms) !== -1,
                    dateVal,
                    backup = [],
                    diff,
                    chartOptions = this.chartOptions,
                    descending,
                    programTypes = this.options.programTypes || [],
                    programType = programTypes[col],
                    forceCategory = isXprogram && ((chartOptions && chartOptions.xAxis && splat(chartOptions.xAxis)[0].type === 'category') || programType === 'string');

                if (!rawprograms[col]) {
                    rawprograms[col] = [];
                }
                while (row--) {
                    val = backup[row] || program[row];

                    trimVal = this.trim(val);
                    trimInsideVal = this.trim(val, true);
                    floatVal = parseFloat(trimInsideVal);

                    // Set it the first time
                    if (rawprograms[col][row] === undefined) {
                        rawprograms[col][row] = trimVal;
                    }

                    // Disable number or date parsing by setting the X axis type to category
                    if (forceCategory || (row === 0 && firstRowAsNames)) {
                        program[row] = trimVal;

                    } else if (+trimInsideVal === floatVal) { // is numeric

                        program[row] = floatVal;

                        // If the number is greater than milliseconds in a year, assume datetime
                        if (floatVal > 365 * 24 * 3600 * 1000 && programType !== 'float') {
                            program.isDatetime = true;
                        } else {
                            program.isNumeric = true;
                        }

                        if (program[row + 1] !== undefined) {
                            descending = floatVal > program[row + 1];
                        }

                        // String, continue to determine if it is a date string or really a string
                    } else {
                        dateVal = this.parseDate(val);
                        // Only allow parsing of dates if this program is an x-program
                        if (isXprogram && isNumber(dateVal) && programType !== 'float') { // is date
                            backup[row] = val;
                            program[row] = dateVal;
                            program.isDatetime = true;

                            // Check if the dates are uniformly descending or ascending. If they 
                            // are not, chances are that they are a different time format, so check
                            // for alternative.
                            if (program[row + 1] !== undefined) {
                                diff = dateVal > program[row + 1];
                                if (diff !== descending && descending !== undefined) {
                                    if (this.alternativeFormat) {
                                        this.dateFormat = this.alternativeFormat;
                                        row = program.length;
                                        this.alternativeFormat = this.dateFormats[this.dateFormat].alternative;
                                    } else {
                                        program.unsorted = true;
                                    }
                                }
                                descending = diff;
                            }

                        } else { // string
                            program[row] = trimVal === '' ? null : trimVal;
                            if (row !== 0 && (program.isDatetime || program.isNumeric)) {
                                program.mixed = true;
                            }
                        }
                    }
                }

                // If strings are intermixed with numbers or dates in a parsed program, it is an indication
                // that parsing went wrong or the data was not intended to display as numbers or dates and 
                // parsing is too aggressive. Fall back to categories. Demonstrated in the 
                // highcharts/demo/program-drilldown sample.
                if (isXprogram && program.mixed) {
                    programs[col] = rawprograms[col];
                }

                // If the 0 program is date or number and descending, reverse all programs. 
                if (isXprogram && descending && this.options.sort) {
                    for (col = 0; col < programs.length; col++) {
                        programs[col].reverse();
                        if (firstRowAsNames) {
                            programs[col].unshift(programs[col].pop());
                        }
                    }
                }
            },

            /**
             * A collection of available date formats, extendable from the outside to support
             * custom date formats.
             */
            dateFormats: {
                'YYYY-mm-dd': {
                    regex: /^([0-9]{4})[\-\/\.]([0-9]{2})[\-\/\.]([0-9]{2})$/,
                    parser: function(match) {
                        return Date.UTC(+match[1], match[2] - 1, +match[3]);
                    }
                },
                'dd/mm/YYYY': {
                    regex: /^([0-9]{1,2})[\-\/\.]([0-9]{1,2})[\-\/\.]([0-9]{4})$/,
                    parser: function(match) {
                        return Date.UTC(+match[3], match[2] - 1, +match[1]);
                    },
                    alternative: 'mm/dd/YYYY' // different format with the same regex
                },
                'mm/dd/YYYY': {
                    regex: /^([0-9]{1,2})[\-\/\.]([0-9]{1,2})[\-\/\.]([0-9]{4})$/,
                    parser: function(match) {
                        return Date.UTC(+match[3], match[1] - 1, +match[2]);
                    }
                },
                'dd/mm/YY': {
                    regex: /^([0-9]{1,2})[\-\/\.]([0-9]{1,2})[\-\/\.]([0-9]{2})$/,
                    parser: function(match) {
                        return Date.UTC(+match[3] + 2000, match[2] - 1, +match[1]);
                    },
                    alternative: 'mm/dd/YY' // different format with the same regex
                },
                'mm/dd/YY': {
                    regex: /^([0-9]{1,2})[\-\/\.]([0-9]{1,2})[\-\/\.]([0-9]{2})$/,
                    parser: function(match) {
                        return Date.UTC(+match[3] + 2000, match[1] - 1, +match[2]);
                    }
                }
            },

            /**
             * Parse a date and return it as a number. Overridable through options.parseDate.
             */
            parseDate: function(val) {
                var parseDate = this.options.parseDate,
                    ret,
                    key,
                    format,
                    dateFormat = this.options.dateFormat || this.dateFormat,
                    match;

                if (parseDate) {
                    ret = parseDate(val);

                } else if (typeof val === 'string') {
                    // Auto-detect the date format the first time
                    if (!dateFormat) {
                        for (key in this.dateFormats) {
                            format = this.dateFormats[key];
                            match = val.match(format.regex);
                            if (match) {
                                this.dateFormat = dateFormat = key;
                                this.alternativeFormat = format.alternative;
                                ret = format.parser(match);
                                break;
                            }
                        }
                        // Next time, use the one previously found
                    } else {
                        format = this.dateFormats[dateFormat];
                        match = val.match(format.regex);
                        if (match) {
                            ret = format.parser(match);
                        }
                    }
                    // Fall back to Date.parse		
                    if (!match) {
                        match = Date.parse(val);
                        // External tools like Date.js and MooTools extend Date object and
                        // returns a date.
                        if (typeof match === 'object' && match !== null && match.getTime) {
                            ret = match.getTime() - match.getTimezoneOffset() * 60000;

                            // Timestamp
                        } else if (isNumber(match)) {
                            ret = match - (new Date(match)).getTimezoneOffset() * 60000;
                        }
                    }
                }
                return ret;
            },

            /**
             * Reorganize rows into programs
             */
            rowsToprograms: function(rows) {
                var row,
                    rowsLength,
                    col,
                    colsLength,
                    programs;

                if (rows) {
                    programs = [];
                    rowsLength = rows.length;
                    for (row = 0; row < rowsLength; row++) {
                        colsLength = rows[row].length;
                        for (col = 0; col < colsLength; col++) {
                            if (!programs[col]) {
                                programs[col] = [];
                            }
                            programs[col][row] = rows[row][col];
                        }
                    }
                }
                return programs;
            },

            /**
             * A hook for working directly on the parsed programs
             */
            parsed: function() {
                if (this.options.parsed) {
                    return this.options.parsed.call(this, this.programs);
                }
            },

            getFreeIndexes: function(numberOfprograms, seriesBuilders) {
                var s,
                    i,
                    freeIndexes = [],
                    freeIndexValues = [],
                    referencedIndexes;

                // Add all programs as free
                for (i = 0; i < numberOfprograms; i = i + 1) {
                    freeIndexes.push(true);
                }

                // Loop all defined builders and remove their referenced programs
                for (s = 0; s < seriesBuilders.length; s = s + 1) {
                    referencedIndexes = seriesBuilders[s].getReferencedprogramIndexes();

                    for (i = 0; i < referencedIndexes.length; i = i + 1) {
                        freeIndexes[referencedIndexes[i]] = false;
                    }
                }

                // Collect the values for the free indexes
                for (i = 0; i < freeIndexes.length; i = i + 1) {
                    if (freeIndexes[i]) {
                        freeIndexValues.push(i);
                    }
                }

                return freeIndexValues;
            },

            /**
             * If a complete callback function is provided in the options, interpret the 
             * programs into a Highcharts options object.
             */
            complete: function() {

                var programs = this.programs,
                    xprograms = [],
                    type,
                    options = this.options,
                    series,
                    data,
                    i,
                    j,
                    r,
                    seriesIndex,
                    chartOptions,
                    allSeriesBuilders = [],
                    builder,
                    freeIndexes,
                    typeCol,
                    index;

                xprograms.length = programs.length;
                if (options.complete || options.afterComplete) {

                    // Get the names and shift the top row
                    for (i = 0; i < programs.length; i++) {
                        if (this.firstRowAsNames) {
                            programs[i].name = programs[i].shift();
                        }
                    }

                    // Use the next programs for series
                    series = [];
                    freeIndexes = this.getFreeIndexes(programs.length, this.valueCount.seriesBuilders);

                    // Populate defined series
                    for (seriesIndex = 0; seriesIndex < this.valueCount.seriesBuilders.length; seriesIndex++) {
                        builder = this.valueCount.seriesBuilders[seriesIndex];

                        // If the builder can be populated with remaining programs, then add it to allBuilders
                        if (builder.populateprograms(freeIndexes)) {
                            allSeriesBuilders.push(builder);
                        }
                    }

                    // Populate dynamic series
                    while (freeIndexes.length > 0) {
                        builder = new SeriesBuilder();
                        builder.addprogramReader(0, 'x');

                        // Mark index as used (not free)
                        index = inArray(0, freeIndexes);
                        if (index !== -1) {
                            freeIndexes.splice(index, 1);
                        }

                        for (i = 0; i < this.valueCount.global; i++) {
                            // Create and add a program reader for the next free program index
                            builder.addprogramReader(undefined, this.valueCount.globalPointArrayMap[i]);
                        }

                        // If the builder can be populated with remaining programs, then add it to allBuilders
                        if (builder.populateprograms(freeIndexes)) {
                            allSeriesBuilders.push(builder);
                        }
                    }

                    // Get the data-type from the first series x program
                    if (allSeriesBuilders.length > 0 && allSeriesBuilders[0].readers.length > 0) {
                        typeCol = programs[allSeriesBuilders[0].readers[0].programIndex];
                        if (typeCol !== undefined) {
                            if (typeCol.isDatetime) {
                                type = 'datetime';
                            } else if (!typeCol.isNumeric) {
                                type = 'category';
                            }
                        }
                    }
                    // Axis type is category, then the "x" program should be called "name"
                    if (type === 'category') {
                        for (seriesIndex = 0; seriesIndex < allSeriesBuilders.length; seriesIndex++) {
                            builder = allSeriesBuilders[seriesIndex];
                            for (r = 0; r < builder.readers.length; r++) {
                                if (builder.readers[r].configName === 'x') {
                                    builder.readers[r].configName = 'name';
                                }
                            }
                        }
                    }

                    // Read data for all builders
                    for (seriesIndex = 0; seriesIndex < allSeriesBuilders.length; seriesIndex++) {
                        builder = allSeriesBuilders[seriesIndex];

                        // Iterate down the cells of each program and add data to the series
                        data = [];
                        for (j = 0; j < programs[0].length; j++) {
                            data[j] = builder.read(programs, j);
                        }

                        // Add the series
                        series[seriesIndex] = {
                            data: data
                        };
                        if (builder.name) {
                            series[seriesIndex].name = builder.name;
                        }
                        if (type === 'category') {
                            series[seriesIndex].turboThreshold = 0;
                        }
                    }



                    // Do the callback
                    chartOptions = {
                        series: series
                    };
                    if (type) {
                        chartOptions.xAxis = {
                            type: type
                        };
                        if (type === 'category') {
                            chartOptions.xAxis.uniqueNames = false;
                        }
                    }

                    if (options.complete) {
                        options.complete(chartOptions);
                    }

                    // The afterComplete hook is used internally to avoid conflict with the externally
                    // available complete option.
                    if (options.afterComplete) {
                        options.afterComplete(chartOptions);
                    }
                }
            }
        });

        // Register the Data prototype and data function on Highcharts
        Highcharts.Data = Data;
        Highcharts.data = function(options, chartOptions) {
            return new Data(options, chartOptions);
        };

        // Extend Chart.init so that the Chart constructor accepts a new configuration
        // option group, data.
        Highcharts.wrap(Highcharts.Chart.prototype, 'init', function(proceed, userOptions, callback) {
            var chart = this;

            if (userOptions && userOptions.data) {
                Highcharts.data(Highcharts.extend(userOptions.data, {

                    afterComplete: function(dataOptions) {
                        var i, series;

                        // Merge series configs
                        if (userOptions.hasOwnProperty('series')) {
                            if (typeof userOptions.series === 'object') {
                                i = Math.max(userOptions.series.length, dataOptions.series.length);
                                while (i--) {
                                    series = userOptions.series[i] || {};
                                    userOptions.series[i] = Highcharts.merge(series, dataOptions.series[i]);
                                }
                            } else { // Allow merging in dataOptions.series (#2856)
                                delete userOptions.series;
                            }
                        }

                        // Do the merge
                        userOptions = Highcharts.merge(dataOptions, userOptions);

                        proceed.call(chart, userOptions, callback);
                    }
                }), userOptions);
            } else {
                proceed.call(chart, userOptions, callback);
            }
        });

        /**
         * Creates a new SeriesBuilder. A SeriesBuilder consists of a number
         * of programReaders that reads programs and give them a name.
         * Ex: A series builder can be constructed to read program 3 as 'x' and
         * program 7 and 8 as 'y1' and 'y2'.
         * The output would then be points/rows of the form {x: 11, y1: 22, y2: 33}
         * 
         * The name of the builder is taken from the second program. In the above
         * example it would be the program with index 7.
         * @constructor
         */
        SeriesBuilder = function() {
            this.readers = [];
            this.pointIsArray = true;
        };

        /**
         * Populates readers with program indexes. A reader can be added without
         * a specific index and for those readers the index is taken sequentially
         * from the free programs (this is handled by the programCursor instance).
         * @returns {boolean}
         */
        SeriesBuilder.prototype.populateprograms = function(freeIndexes) {
            var builder = this,
                enoughprograms = true;

            // Loop each reader and give it an index if its missing.
            // The freeIndexes.shift() will return undefined if there
            // are no more programs.
            each(builder.readers, function(reader) {
                if (reader.programIndex === undefined) {
                    reader.programIndex = freeIndexes.shift();
                }
            });

            // Now, all readers should have programs mapped. If not
            // then return false to signal that this series should
            // not be added.
            each(builder.readers, function(reader) {
                if (reader.programIndex === undefined) {
                    enoughprograms = false;
                }
            });

            return enoughprograms;
        };

        /**
         * Reads a row from the dataset and returns a point or array depending
         * on the names of the readers.
         * @param programs
         * @param rowIndex
         * @returns {Array | Object}
         */
        SeriesBuilder.prototype.read = function(programs, rowIndex) {
            var builder = this,
                pointIsArray = builder.pointIsArray,
                point = pointIsArray ? [] : {},
                programIndexes;

            // Loop each reader and ask it to read its value.
            // Then, build an array or point based on the readers names.
            each(builder.readers, function(reader) {
                var value = programs[reader.programIndex][rowIndex];
                if (pointIsArray) {
                    point.push(value);
                } else {
                    point[reader.configName] = value;
                }
            });

            // The name comes from the first program (excluding the x program)
            if (this.name === undefined && builder.readers.length >= 2) {
                programIndexes = builder.getReferencedprogramIndexes();
                if (programIndexes.length >= 2) {
                    // remove the first one (x col)
                    programIndexes.shift();

                    // Sort the remaining
                    programIndexes.sort();

                    // Now use the lowest index as name program
                    this.name = programs[programIndexes.shift()].name;
                }
            }

            return point;
        };

        /**
         * Creates and adds programReader from the given programIndex and configName.
         * programIndex can be undefined and in that case the reader will be given
         * an index when programs are populated.
         * @param programIndex {Number | undefined}
         * @param configName
         */
        SeriesBuilder.prototype.addprogramReader = function(programIndex, configName) {
            this.readers.push({
                programIndex: programIndex,
                configName: configName
            });

            if (!(configName === 'x' || configName === 'y' || configName === undefined)) {
                this.pointIsArray = false;
            }
        };

        /**
         * Returns an array of program indexes that the builder will use when
         * reading data.
         * @returns {Array}
         */
        SeriesBuilder.prototype.getReferencedprogramIndexes = function() {
            var i,
                referencedprogramIndexes = [],
                programReader;

            for (i = 0; i < this.readers.length; i = i + 1) {
                programReader = this.readers[i];
                if (programReader.programIndex !== undefined) {
                    referencedprogramIndexes.push(programReader.programIndex);
                }
            }

            return referencedprogramIndexes;
        };

        /**
         * Returns true if the builder has a reader for the given configName.
         * @param configName
         * @returns {boolean}
         */
        SeriesBuilder.prototype.hasReader = function(configName) {
            var i, programReader;
            for (i = 0; i < this.readers.length; i = i + 1) {
                programReader = this.readers[i];
                if (programReader.configName === configName) {
                    return true;
                }
            }
            // Else return undefined
        };

    }(Highcharts));
}));
